package likong.bibliothek.pages.autor;

import org.apache.wicket.PageParameters;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import likong.bibliothek.obj.Autor;

import db.HibernateHelper;

public class AutorLoeschen extends AutorAbstract {
	public AutorLoeschen(final PageParameters params) {
		
		int autorID;
		Autor autor;
		try {
			autorID = params.getInt("autorID");
			//autor = Autor.fromID(autorID);
			autor = HibernateHelper.retrieveObject(Autor.class, autorID);
		} catch (Exception e) {
			this.setRedirect(true);
			this.setResponsePage(AutorWeb.class);
			return;
		}
		
		final CompoundPropertyModel<Autor> model = new CompoundPropertyModel<Autor>(autor);
		
		AutorFormPanel form = new AutorFormPanel("loescheAutorFormular", model, "Autor löschen", false) {
			private static final long serialVersionUID = 1L;
			
			@Override
			protected void onSubmitModelHandle(IModel<Autor> model) {
				HibernateHelper.delete(model.getObject());
				
				this.setRedirect(true);
				this.setResponsePage(AutorWeb.class);
			}

			@Override
			protected void onBackButtonClick() {
				this.setRedirect(true);
				this.setResponsePage(AutorWeb.class, params);
			}
		}; 
		
		add (form);
		
	}
}
